'use strict';

import React, {Component} from "react";
import PropTypes from "prop-types";
import {
    Alert,
    BackAndroid,
    InteractionManager,
    ListView,
    Platform,
    StatusBar,
    Text,
    TouchableOpacity,
    View
} from "react-native";
import MainUI from "./main/MainUI";
import AuctionDetail from "./main/AuctionDetail";
import chjStyle from "./CustomStyle";
import Util from "./main/Util";
import {ActionConst, Actions, Modal, Reducer, Router, Scene} from "react-native-router-flux";
import CarsOfShop from "./main/mainUI/CarsOfShop";
import ChjGallery from "./main/mainUI/components/ChjGallery";
import MoreCarParams from "./main/mainUI/MoreCarParams";
import CiasLisenceDetail from "./main/mainUI/CiasLisenceDetail";
import CiasLisenceDetail1 from "./main/mainUI/CiasLisenceDetail1";
import CiasLisenceDetail2 from "./main/mainUI/CiasLisenceDetail2";
import CiasLisenceDetail3 from "./main/mainUI/CiasLisenceDetail3";
import CiasLisenceDetail4 from "./main/mainUI/CiasLisenceDetail4";
import MoreCarReports from "./main/mainUI/MoreCarReports";
import Password from "./page/mine/Password";
import RecordedCarList from "./main/record/RecordedCarList";
import BaseInfo from "./main/record/cias/BaseInfo";
import ExternalEngineCheck from "./main/record/qainfo/ExternalEngineCheck";
import CarBrandSelection from "./main/record/common/CarBrandSelection";
import CarModelSelection from "./main/record/common/CarModelSelection";
import CarSourceSelector from "./main/record/common/CarSourceSelector";
import CommonListSelection from "./main/record/common/CommonListSelection";
import CarConfig from "./main/record/cias/CarConfig";
import CarSeriesSelection from "./main/record/common/CarSeriesSelection";
import CarFrame from "./main/record/cias/CarFrame";
import CommonMultiListSelection from "./main/record/common/CommonMultiListSelection";
import CarFace from "./main/record/cias/CarFace";
import CommonPhotoSelection from "./main/record/common/CommonPhotoSelection";
import CarEngine from "./main/record/cias/CarEngine";
import CarCockpit from "./main/record/cias/CarCockpit";
import CarStart from "./main/record/cias/CarStart";
import CarRoadtest from "./main/record/cias/CarRoadtest";
import CarBottom from "./main/record/cias/CarBottom";
import CarFunparts from "./main/record/cias/CarFunparts";
import CarPicture from "./main/record/cias/CarPicture";
import {connect} from "react-redux";
import * as MineAction from "./redux/action/Mine";
import WarrantyOilCheck from "./main/record/qainfo/WarrantyOilCheck";
import PluginEngineCheck from "./main/record/qainfo/PluginEngineCheck";
import WarrantyStartCheck from "./main/record/qainfo/WarrantyStartCheck";
import WarrantyInfo from "./main/record/qainfo/WarrantyInfo";
import WarrantyRoadeTest from "./main/record/qainfo/WarrantyRoadeTest";
import QAStart from "./main/record/qainfo/QAStart";
import UnderpanOne from "./main/record/qainfo/UnderpanOne";
import UnderpanTwo from "./main/record/qainfo/UnderpanTwo";
import DriveRoomOne from "./main/record/qainfo/DriveRoomOne";
import DriveRoomTwo from "./main/record/qainfo/DriveRoomTwo";
import RoadTest from "./main/record/qainfo/RoadTest";
import EngineRoom from "./main/record/qainfo/EngineRoom";
import WebViewPage from "./page/WebViewPage";
import NewsUI from "./main/mainUI/NewsUI";
import BrandSelect from "./page/buyCar/Brand";
import Order from "./page/buyCar/Order";
import Price from "./page/buyCar/Price";
import Filter from "./page/buyCar/Filter";
import LoanUI from "./page/buyCar/LoanUI";
import ApplyLoan from "./page/buyCar/ApplyLoan";
import SearchByKeyword from "./page/buyCar/SearchByKeyword";
import DetailKind from "./page/buyCar/DetailKind";
import Init from "./config/Init";
import Settings from "./page/mine/Setting";
import Login from "./page/mine/LoginUI";
import MyEstimate from "./page/mine/MyEstimate";
import ViewCollect from "./page/mine/ViewCollect";
import Register from "./page/mine/Register";
import ShopSearch from "./page/shop/Search";
import ShopDetail from "./page/shop/ShopDetail";
import OrderSoldCarUI from "./page/mine/OrderSoldCarUI";
import CarDetail from "./page/cardetail/CarDetail";
import AllParam from "./page/cardetail/AllParam";
import CheckParamDetail from "./page/cardetail/CheckParamDetail";
import CarGallery from "./page/cardetail/CarGallery";
import CheckGallery from "./page/cardetail/CheckGallery";
import CarGalleryAll from "./page/cardetail/CarGalleryAll";
import Accredit from "./page/accredit/Accredit";
import HelpLook from "./page/helplook/HelpLook";
import HelpCollectList from "./page/helplook/HelpCollectList";
import HelpLookDetail from "./page/helplook/HelpLookDetail";
import RecordList from "./page/helplook/record/RecordList";
import ServerDetail from "./page/helplook/ServerDetail";
import HelpCollect from "./page/helplook/HelpCollect";
import HelpCheckParamDetail from "./page/helplook/CheckParamDetail";
import HelpAllParam from "./page/helplook/AllParam";
import CarsForAuctionUI from "./main/mainUI/CarsForAuctionUI";
import CustomStyle from "../common/style/CustomStyle";
import LoadingText from "./components/LoadingText";
import HelpBaseInfo from "./page/helplook/record/HelpBaseInfo";
import HelpCarConfig from "./page/helplook/record/HelpCarConfig";
import HelpCarFrame from "./page/helplook/record/HelpCarFrame";
import HelpCarFace from "./page/helplook/record/HelpCarFace";
import HelpCarEngine from "./page/helplook/record/HelpCarEngine";
import HelpCarCockpit from "./page/helplook/record/HelpCarCockpit";
import HelpCarStart from "./page/helplook/record/HelpCarStart";
import HelpCarFunparts from "./page/helplook/record/HelpCarFunparts";
import HelpCarPicture from "./page/helplook/record/HelpCarPicture";

const DATA_SOURCE = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
const RouterWithRedux = connect()(Router);

class ChjNavigator extends React.Component {

    constructor(props) {
        super(props);

        this.state = {};

        Util.storage.load({
            key: 'users',
            id: '1000'
        }).then(ret => {
            const id = ret.id ? ret.id : ret.userid;
            const info = ret.info ? ret.info : ret.userinfo;
            this.props.updateUserData({
                id,
                token: ret.token,
                phone: ret.phone,
                info,
            });
        }).catch(err => {
        });
    }

    onBackAndroid = () => {
        Alert.alert('车行家-二手车专业市场', '点击确定退出', [
            {
                text: '取消', onPress: () => {
            }
            },
            {text: '确定', onPress: () => BackAndroid.exitApp()},
        ]);
        return true;
    }

    componentWillUnmount() {
        if (Platform.OS === 'android') {
            BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid);
        }
    }

    componentDidMount() {
        Init();
        Actions.loading();
    }

    render() {
        return (
            <View style={chjStyle.mainContainer}>
                <StatusBar
                    barStyles='light-content'
                    backgroundColor='#00000000'
                    hidden={false}
                    animated={false}
                    translucent={chjStyle.openTranslucent}
                />
                <RouterWithRedux
                    createReducer={reducerCreate}
                    onExitApp={() => {
                        this.onBackAndroid();
                        return true;
                    }}
                >
                    <Scene
                        key="modal" component={Modal}
                    >
                        <Scene key="root" hideNavBar hideTabBar>
                            <Scene key="'main" component={MainUI} initial={true} type={ActionConst.RESET}/>

                            <Scene key='Settings' component={Settings}/>
                            <Scene key='CarsForAuctionUI' component={CarsForAuctionUI}/>
                            <Scene key='Accredit' component={Accredit}/>
                            <Scene key='HelpLook' component={HelpLook}/>
                            <Scene key='HelpLookDetail' component={HelpLookDetail}/>
                            <Scene key='HelpCollectList' component={HelpCollectList}/>
                            <Scene key='ServerDetail' component={ServerDetail}/>
                            <Scene key='HelpCollect' component={HelpCollect}/>
                            <Scene key='HelpAllParam' component={HelpAllParam}/>
                            <Scene key='HelpCheckParamDetail' component={HelpCheckParamDetail}/>

                            <Scene key='CarDetail' component={CarDetail}/>
                            <Scene key='AllParam' component={AllParam}/>
                            <Scene key='CheckParamDetail' component={CheckParamDetail}/>
                            <Scene key='CarGallery' component={CarGallery}/>
                            <Scene key='CarGalleryAll' component={CarGalleryAll}/>
                            <Scene key='CheckGallery' component={CheckGallery}/>
                            <Scene key='CarsOfShop' component={CarsOfShop}/>
                            <Scene key='ChjGallery' component={ChjGallery}/>
                            <Scene key='MoreCarParams' component={MoreCarParams}/>
                            <Scene key='CiasLisenceDetail' component={CiasLisenceDetail}/>
                            <Scene key='CiasLisenceDetail1' component={CiasLisenceDetail1}/>
                            <Scene key='CiasLisenceDetail2' component={CiasLisenceDetail2}/>
                            <Scene key='CiasLisenceDetail3' component={CiasLisenceDetail3}/>
                            <Scene key='CiasLisenceDetail4' component={CiasLisenceDetail4}/>
                            <Scene key='MoreCarReports' component={MoreCarReports}/>
                            <Scene key="Mine" hideNavBar hideTabBar>
                                <Scene key='Login' component={Login}/>
                                <Scene key='Register' component={Register}/>
                                <Scene key='Password' component={Password}/>
                            </Scene>
                            <Scene key='ViewCollect' component={ViewCollect}/>
                            <Scene key='MyEstimate' component={MyEstimate}/>

                            <Scene key='RecordList' initial={false} component={RecordList}/>

                            <Scene key="RecordSimpleRoot" passProps={true} hideNavBar hideTabBar>
                                <Scene key='HelpBaseInfo' component={HelpBaseInfo}/>
                                <Scene key='HelpCarConfig' component={HelpCarConfig}/>
                                <Scene key='HelpCarFace' component={HelpCarFace}/>
                                <Scene key='HelpCarFrame' component={HelpCarFrame}/>
                                <Scene key='HelpCarEngine' component={HelpCarEngine}/>
                                <Scene key='HelpCarCockpit' component={HelpCarCockpit}/>
                                <Scene key='HelpCarStart' component={HelpCarStart}/>
                                <Scene key='HelpCarFunparts' component={HelpCarFunparts}/>
                                <Scene key='HelpCarPicture' component={HelpCarPicture}/>
                            </Scene>

                            <Scene key='RecordedCarList' component={RecordedCarList}/>
                            <Scene key='BaseInfo' component={BaseInfo}/>
                            <Scene key='ExternalEngineCheck' component={ExternalEngineCheck}/>
                            <Scene key='CarBrandSelection' component={CarBrandSelection}/>
                            <Scene key='CarModelSelection' component={CarModelSelection}/>
                            <Scene key='CarSourceSelector' component={CarSourceSelector}/>
                            <Scene key='CommonListSelection' component={CommonListSelection}/>
                            <Scene key='CarConfig' component={CarConfig}/>
                            <Scene key='CarSeriesSelection' component={CarSeriesSelection}/>
                            <Scene key='CarFrame' component={CarFrame}/>
                            <Scene key='CommonMultiListSelection' component={CommonMultiListSelection}/>
                            <Scene key='CarFace' component={CarFace}/>
                            <Scene key='CommonPhotoSelection' component={CommonPhotoSelection}/>
                            <Scene key='CarEngine' component={CarEngine}/>
                            <Scene key='CarCockpit' component={CarCockpit}/>
                            <Scene key='CarStart' component={CarStart}/>
                            <Scene key='CarRoadtest' component={CarRoadtest}/>
                            <Scene key='CarBottom' component={CarBottom}/>
                            <Scene key='CarFunparts' component={CarFunparts}/>
                            <Scene key='CarPicture' component={CarPicture}/>
                            <Scene key='WarrantyOilCheck' component={WarrantyOilCheck}/>
                            <Scene key='PluginEngineCheck' component={PluginEngineCheck}/>
                            <Scene key='WarrantyStartCheck' component={WarrantyStartCheck}/>
                            <Scene key='WarrantyRoadeTest' component={WarrantyRoadeTest}/>
                            <Scene key='WarrantyInfo' component={WarrantyInfo}/>

                            <Scene key='WebViewPage' component={WebViewPage}/>
                            <Scene key='NewsUI' component={NewsUI}/>

                            <Scene key='Order' component={Order}/>
                            <Scene key='Price' component={Price}/>
                            <Scene key='Filter' component={Filter}/>
                            <Scene key='LoanUI' component={LoanUI}/>
                            <Scene key='ApplyLoan' component={ApplyLoan}/>
                            <Scene key='SearchByKeyword' component={SearchByKeyword}/>

                            <Scene key="BrandRoot" passProps={true} hideNavBar hideTabBar>
                                <Scene key='BrandSelect' component={BrandSelect}/>
                                <Scene key='DetailKind' component={DetailKind}/>
                            </Scene>

                            <Scene key='ShopSearch' component={ShopSearch}/>
                            <Scene key='ShopDetail' component={ShopDetail}/>
                            <Scene key='OrderSoldCarUI' component={OrderSoldCarUI}/>

                            <Scene key="QaRoot" passProps={true} hideNavBar hideTabBar>
                                <Scene key='EngineRoom' component={EngineRoom}/>
                                <Scene key='DriveRoomOne' component={DriveRoomOne}/>
                                <Scene key='DriveRoomTwo' component={DriveRoomTwo}/>
                                <Scene key='QAStart' component={QAStart}/>
                                <Scene key='UnderpanOne' component={UnderpanOne}/>
                                <Scene key='UnderpanTwo' component={UnderpanTwo}/>
                                <Scene key='RoadTest' component={RoadTest}/>
                            </Scene>

                        </Scene>
                        <Scene key='loading' component={LoadingText}/>
                        <Scene key='dialog' component={Dialog}/>

                        <Scene key='AuctionDetail' component={AuctionDetail}/>
                    </Scene>

                </RouterWithRedux>
            </View>
        );
    }

}


class Dialog extends Component {

    static propTypes = {
        array: PropTypes.array,
        select: PropTypes.func,
    };

    static defaultProps = {
        array: [],
        select: () => {
        },
    };

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            hide: props.hide,
        };
        this.renderRow = this.renderRow.bind(this);
    }

    renderRow(item, i) {
        return (<TouchableOpacity activeOpacity={0.8}
                                  style={[chjStyle.textItem, {alignItems: 'center', justifyContent: 'center'}]}
                                  onPress={() => {
                                      Actions.pop();
                                      InteractionManager.runAfterInteractions(() => {
                                          this.props.select(item, i);
                                      });
                                  }}>
            <Text
                adjustsFontSizeToFit={true}
                style={{
                    color: '#303030',
                    textAlign: 'center',
                    fontSize: chjStyle.getActualFontSize(30),
                    width: chjStyle.getActualSizeInHorizontal(670),
                }}>{item.name}</Text>
        </TouchableOpacity>);
    }

    render() {
        return (
            <TouchableOpacity
                activeOpacity={1}
                style={[CustomStyle.modalContainer]}
                onPress={() => this.setState({hide: true})}>
                <ListView style={[chjStyle.dialogContainer]}
                          dataSource={DATA_SOURCE.cloneWithRows(this.props.array)}
                          renderRow={this.renderRow}
                />
            </TouchableOpacity>
        );
    }
}

const reducerCreate = params => {
    const defaultReducer = new Reducer(params);
    return (state, action) => {
        console.log('chjNavigator  scene key ' + action.key);
        return defaultReducer(state, action);
    };
};

const mapStateToProps = (state) => {
    return {};
};

const mapDispatchToProps = (dispatch) => {
    return {
        updateUserData: (param) => {
            dispatch(MineAction.userData(param));
        },
    };
};

export default connect(mapStateToProps, mapDispatchToProps)(ChjNavigator);
